import { StatusFilterDto } from './dto/status-filter.dto';
import { TaskStatus } from './task-status.enum';
import {
  Injectable,
  NotFoundException,
  BadRequestException,
} from '@nestjs/common';
import CreateTaskDto from './dto/create-task.dto';
import UpdateTaskDto from './dto/update-task.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Task } from './task.entity';
import { TaskRepository } from './task.repository';

@Injectable()
export class TasksService {
  constructor(
    @InjectRepository(TaskRepository)
    private taskRepository: TaskRepository,
  ) {}
  async getTaskById(id: number): Promise<Task> {
    const found = await this.taskRepository.findOne(id);
    if (!found) {
      
      throw new NotFoundException(`找不到id为${id}的task`);
    }
    return found;
  }
  // private tasks: Task[] = [];
  // getAllTasks(filter?: StatusFilterDto): Task[] {
  //   if (filter && Object.keys(filter).length > 0) {
  //     return this.tasks.filter((task) => {
  //       console.log(filter.status, 'filter.status');
  //       if (task.status.includes(filter.status)) {
  //         return true;
  //       }
  //       return false;
  //     });
  //   }
  //   return this.tasks;
  // }
  async createtask(creatTaskDto: CreateTaskDto): Promise<Task> {
    const { title, description } = creatTaskDto;
    const task = new Task();
    task.title = title;
    task.description = description;
    task.status = TaskStatus.OPEN;
    await task.save();
    return task;
  }
  // getTaskById(id): Task {
  //   const found = this.tasks.find((task) => task.id === id);
  //   console.log('asdad');
  //   if (found) {
  //     return found;
  //   }
  //   throw new NotFoundException(`Task With ID ${id} NOT FOUND`);
  // }
  // deleteTaskById(id): Task {
  //   const task = this.getTaskById(id);
  //   const idx = this.tasks.indexOf(task);
  //   this.tasks.splice(idx, 1);
  //   return task;
  // }
  // updateTaskById(id, newTask: UpdateTaskDto): Task {
  //   const task = this.getTaskById(id);
  //   const idx = this.tasks.indexOf(task);
  //   newTask.id = id;
  //   newTask.status = TaskStatus.IN_PROGRESS;
  //   this.tasks[idx] = newTask;
  //   return this.tasks[idx];
  // }
  // // 只是为了配合 一个 自定义pipe瞎写的
  // updateStatusById(id, status: TaskStatus) {
  //   const task = this.getTaskById(id);
  //   const idx = this.tasks.indexOf(task);
  //   task.status = status;
  //   this.tasks[idx] = task;
  //   return task;
  // }
}
